import { http } from '@/utils/http'
import { defaultTo } from 'lodash-es'

/** 查询用户列表 */
export const listUser = (params?: object) => {
  return http.request<HttpPaginationResponse<Array<any>>>('get', '/system/user/list', {
    params,
  })
}

/** 查询用户详细 */
export const getUser = (params?: object, userId?: number) => {
  return http.request<HttpResponse<Object>>('get', '/system/user/' + defaultTo(userId, ''), {
    params,
  })
}

/** 新增用户 */
export const addUser = (data?: object) => {
  return http.request<HttpResponse<Object>>('post', '/system/user', { data })
}

/** 修改用户 */
export const updateUser = (data?: object) => {
  return http.request<HttpResponse<Object>>('put', '/system/user', { data })
}

/** 删除用户 */
export const delUser = (userId?: number) => {
  return http.request<HttpResponse<Object>>('delete', '/system/user/' + userId)
}

/** 用户密码重置 */
export const resetUserPwd = (userId: number, password: string) => {
  const data = {
    userId,
    password,
  }
  return http.request<HttpResponse<Object>>('put', '/system/user/resetPwd', {
    data,
  })
}

/** 用户状态修改 */
export const changeUserStatus = (userId: number, status: string) => {
  const data = {
    userId,
    status,
  }
  return http.request<HttpResponse<Object>>('put', '/system/user/changeStatus', { data })
}

/** 用户状态修改 */
export const getUserProfile = () => {
  return http.request<HttpResponse<Object>>('get', '/system/user/profile')
}

/** 修改用户个人信息 */
export const updateUserProfile = (data?: object) => {
  return http.request<HttpResponse<Object>>('put', '/system/user/profile', {
    data,
  })
}

/** 用户密码重置 */
export const updateUserPwd = (oldPassword: string, newPassword: string) => {
  const data = {
    oldPassword,
    newPassword,
  }
  return http.request<HttpResponse<Object>>('put', '/system/user/profile/updatePwd', {
    data,
  })
}

/** 用户头像上传 */
export const uploadAvatar = (data?: object) => {
  return http.request<HttpResponse<Object>>('post', '/system/user/profile/avatar', { data })
}

/** 查询授权角色 */
export const getAuthRole = (params?: object, userId?: number) => {
  return http.request<HttpResponse<Object>>('get', '/system/user/authRole/' + userId, {
    params,
  })
}

/** 保存授权角色 */
export const updateAuthRole = (params?: object) => {
  return http.request<HttpResponse<Object>>('put', '/system/user/authRole', {
    params,
  })
}

/** 查询部门下拉树结构 */
export const deptTreeSelect = () => {
  return http.request<HttpResponse<Object>>('get', '/system/user/deptTree')
}
